//
//  UIImage+Blur.m
//  xpkc
//
//  Created by TopSageOSX on 15/3/6.
//  Copyright (c) 2015年 TopSage. All rights reserved.
//

#import "UIImage+Blur.h"
#import <QuartzCore/QuartzCore.h>
#import <Accelerate/Accelerate.h>
@implementation UIImage (Blur)

- (UIImage *)blurImageWithBlur:(CGFloat)blur{
//    CIImage *inputImage = [CIImage imageWithCGImage:self.CGImage];
//    CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"
//                                  keysAndValues:kCIInputImageKey, inputImage,
//                        @"inputRadius", @(blur),
//                        nil];
//    
//    CIImage *outputImage = filter.outputImage;
//    
//    CGImageRef outImage;
//    outImage= [self.context createCGImage:outputImage
//                                             fromRect:[outputImage extent]];
//    return [UIImage imageWithCGImage:outImage];
    return [self blurImageWithBlur:blur andTintColor:nil];
}
- (UIImage *)blurImageWithBlur:(CGFloat)blur andTintColor:(UIColor *)tintColor{
    if (blur < 0.0f||blur > 1.0f) {
        blur = 0.5f;
    }
    int blurSize = (int)(blur * 40);
    blurSize = blurSize - (blurSize % 2) +1;
    
    CGImageRef image = self.CGImage;
    vImage_Buffer inBuffer,outBuffer;
    vImage_Error imageError;
    void *pixelBuffer;
    
    CGDataProviderRef providerRef = CGImageGetDataProvider(image);
    CFDataRef inBitMapData = CGDataProviderCopyData(providerRef);
    
    inBuffer.width = CGImageGetWidth(image);
    inBuffer.height = CGImageGetHeight(image);
    inBuffer.rowBytes = CGImageGetBytesPerRow(image);
    
    inBuffer.data = (void *)CFDataGetBytePtr(inBitMapData);
    
    pixelBuffer = malloc(CGImageGetBytesPerRow(image) * CGImageGetHeight(image));
    if (pixelBuffer == NULL) {
        NSLog(@"No PixelBuffer");
    }
    
    outBuffer.data = pixelBuffer;
    outBuffer.width = CGImageGetWidth(image);
    outBuffer.height = CGImageGetHeight(image);
    outBuffer.rowBytes = CGImageGetBytesPerRow(image);
    
    imageError =
    vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer, NULL, 0, 0, blurSize, blurSize, NULL, kvImageEdgeExtend);
    
    if (imageError) {
        NSLog(@"image error from evolution %ld",imageError);
    }
    
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGContextRef ctx = CGBitmapContextCreate(outBuffer.data, outBuffer.width, outBuffer.height, 8, outBuffer.rowBytes, colorSpaceRef, (CGBitmapInfo)kCGImageAlphaPremultipliedFirst);
    
    CGRect rect = {CGPointZero,self.size};
    
//    if (tintColor) {
//        CGContextSaveGState(ctx);
//        CGContextSetFillColorWithColor(ctx, tintColor.CGColor);
//        CGContextFillRect(ctx, rect);
//        CGContextRestoreGState(ctx);
//    }
    
    CGImageRef imageRef = CGBitmapContextCreateImage(ctx);
    
    UIImage *returnImage = [UIImage imageWithCGImage:imageRef];
    
    //释放
    CGContextRelease(ctx);
    CGColorSpaceRelease(colorSpaceRef);
    free(pixelBuffer);
    CFRelease(inBitMapData);
    CGImageRelease(imageRef);
    
    return returnImage;
}
@end
